package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.ProPurchase;
import com.woniu.mapper.ProPurchaseMapper;
import com.woniu.service.IProPurchaseService;
import com.woniu.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *<p>
 *采购的实现类
 *</p>
 * @author ZhouWu
 * @date 2022/8/30 16:37
 */

@Service
@Transactional
//@SuppressWarnings("all")
public class ProPurchaseServiceImpl extends ServiceImpl<ProPurchaseMapper, ProPurchase> implements IProPurchaseService {

    @Autowired
    private ProPurchaseMapper proPurchaseMapper;

    @Override
    public void add(ProPurchase proPurchase) {
        proPurchaseMapper.add(proPurchase);
    }

    @Override
    public void delete(int purId) {
        proPurchaseMapper.delete(purId);
    }

    @Override
    public void update(ProPurchase proPurchase) {
        proPurchaseMapper.update(proPurchase);
    }


    @Override
    public ProPurchase getByPurId(int purId) {
        return proPurchaseMapper.getByPurId(purId);
    }

    @Override
    public List<ProPurchase> getAll() {
        List<ProPurchase> purchases = new ArrayList<>();
        purchases = proPurchaseMapper.getAll();
        return purchases;
    }

    @Override
    public List<ProPurchase> getByProId(int proId) {
        List<ProPurchase> purchases = new ArrayList<>();
        purchases = proPurchaseMapper.getByProId(proId);
        return purchases;
    }


    @Override
    public Page<ProPurchase> getProPurchaseByPage(Integer pageNum, Integer pageSize, ProPurchase proPurchase) {
        QueryWrapper wrapper = getWrapper(proPurchase);
        Page<ProPurchase> page = new Page<>(pageNum, pageSize);
        return proPurchaseMapper.getProPurchaseByPage(page,wrapper);
    }

    @Override
    public List<ProPurchase> getProPurchaseByCondition(ProPurchase proPurchase) {
        List<ProPurchase> purchases = new ArrayList<>();
        QueryWrapper wrapper = getWrapper(proPurchase);
        purchases = proPurchaseMapper.getProPurchaseByCondition(wrapper);
        return purchases;
    }

    private static QueryWrapper getWrapper(ProPurchase proPurchase) {
        QueryWrapper<ProPurchase> wrapper = new QueryWrapper<>();
        if (proPurchase == null){
            wrapper.eq("1","1");
            return wrapper;
        };
        if (EmptyUtil.isNoEmpty(proPurchase.getProject())) {
            wrapper.eq("pro_id", proPurchase.getProject().getProId());
        };
        if (proPurchase.getPurName() != null ) {
            wrapper.eq("pur_name", proPurchase.getPurName());
        };
        if (proPurchase.getPurDetail() != null ) {
            wrapper.eq("pur_detail", proPurchase.getPurDetail());
        };
        if (proPurchase.getSupplier() != null ) {
            wrapper.eq("supplier", proPurchase.getSupplier());
        };
        if (proPurchase.getPurPrice() != null ) {
            wrapper.eq("pur_price", proPurchase.getPurPrice());
        };
        if (EmptyUtil.isNoEmpty(proPurchase.getPurNum()) ) {
            wrapper.eq("pur_num", proPurchase.getPurNum());
        };
        if (proPurchase.getPurDate() != null ) {
            wrapper.eq("pur_date", proPurchase.getPurDate());
        };
        return wrapper;
    }

}
